var Net = require("Net")
// var Global = require("Global")
cc.Class({
    extends: cc.Component,

    properties: {
        lblName:cc.Label,
        lblGems:cc.Label,
        lblID:cc.Label,
        lblNotice:cc.Label,
        joinGameWin:cc.Node,
        createRoomWin:cc.Node,
        settingsWin:cc.Node,
        helpWin:cc.Node,
        xiaoxiWin:cc.Node,
        btnJoinGame:cc.Node,
        btnReturnGame:cc.Node,
        sprHeadImg:cc.Sprite,
        shareNode:cc.Node
        // foo: {
        //    default: null,
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
    },
    
    onShare:function(){
        this.shareNode.active=true
    },
    onCloseShare:function(){
        this.shareNode.active=false
    },
    // 分享到微信好友
    onShareHaoyou:function(){
        this.shareNode.active=false;
        cc.vv.anysdkMgr.share("天地人和","最地道的景德镇棋牌，大家都在玩","haoyou")
    },
    // 分享到微信朋友圈
    onSharePengyou:function(){
        this.shareNode.active=false;
        // 测试
        // cc.vv.anysdkMgr.onShareResp();
        cc.vv.anysdkMgr.share("天地人合,景德镇最地道的棋牌游戏,诚招代理,快快加入吧。","","pengyou")
    },
    // use this for initialization
    onLoad: function () {
        console.log("MjHallOnLoad");
        if(!cc.sys.isNative && cc.sys.isMobile){
            var cvs = this.node.getComponent(cc.Canvas);
            cvs.fitHeight = true;
            cvs.fitWidth = true
        };
        if(!cc.vv){
            cc.director.loadScene("loading");
            return
        };
        this.initLabels();
        
        if(cc.vv.gameNetMgr.roomId == null){
            this.btnJoinGame.active = true;
            this.btnReturnGame.active = false
        }else{
            this.btnJoinGame.active = false;
            this.btnReturnGame.active = true
        };
        
        //var params = cc.vv.args;
        var roomId = cc.vv.userMgr.oldRoomId 
        if( roomId != null){
            cc.vv.userMgr.oldRoomId = null;
            cc.vv.userMgr.enterRoom(roomId)
        };
        
        var imgLoader = this.sprHeadImg.node.getComponent("ImageLoader");
        imgLoader.setUserID(cc.vv.userMgr.userId);
        cc.vv.utils.addClickEvent(this.sprHeadImg.node,this.node,"MjHall","onBtnClicked");
        
        
        this.addComponent("UserInfoShow");
        
        this.initButtonHandler("Canvas/right_bottom/btn_shezhi");
        this.initButtonHandler("Canvas/right_bottom/btn_help");
        this.initButtonHandler("Canvas/right_bottom/btn_xiaoxi");
        this.helpWin.addComponent("OnBack");
        this.xiaoxiWin.addComponent("OnBack");
        // 俱乐部按钮
        var clubBt = this.node.getChildByName("btnClub");
        cc.vv.utils.addClickEvent(clubBt,this.node,"MjHall","onClubBtnClick");
        // 分享界面
        cc.vv.utils.addClickEvent(this.shareNode,this.node,"MjHall","onCloseShare");
        var closeNode = this.shareNode.getChildByName("close");
        cc.vv.utils.addClickEvent(closeNode,this.node,"MjHall","onCloseShare");
        // 分享按钮
        var shareBt = this.node.getChildByName("btn_share");
        cc.vv.utils.addClickEvent(shareBt,this.node,"MjHall","onShare");
        // 分享好友按钮
        var haoyouBt = this.shareNode.getChildByName("haoyou");
        cc.vv.utils.addClickEvent(haoyouBt,this.node,"MjHall","onShareHaoyou");
        // 分享朋友圈按钮
        var pengyouBt = this.shareNode.getChildByName("pengyouquan");
        cc.vv.utils.addClickEvent(pengyouBt,this.node,"MjHall","onSharePengyou");

        // 创建房间按钮
        var createBt = this.node.getChildByName("btn_create_room");
        cc.vv.utils.addClickEvent(createBt,this.node,"MjHall","onCreateRoomClicked");
        // 加入房间按钮
        var joinBt = this.node.getChildByName("btnJoinGame");
        cc.vv.utils.addClickEvent(joinBt,this.node,"MjHall","onJoinGameClicked");
        // 返回房间按钮
        var returnBt = this.node.getChildByName("btnReturnGame");
        cc.vv.utils.addClickEvent(returnBt,this.node,"MjHall","onReturnGameClicked");
        if(!cc.vv.userMgr.notice){
            cc.vv.userMgr.notice = {
                version:null,
                msg:"数据请求中..."
            }
        };
        
        if(!cc.vv.userMgr.gemstip){
            cc.vv.userMgr.gemstip = {
                version:null,
                msg:"数据请求中..."
            }
        };
        this.lblNotice.string = cc.vv.userMgr.notice.msg;
        this.refreshInfo();
        this.refreshNotice();
        this.refreshGemsTip();
        cc.vv.audioMgr.playBGM("hall_bg.mp3");
        
        // 是否俱乐部房间
        if (cc.vv.gameNetMgr.clubid) {
            console.log("俱乐部房间返回大厅");
            this.onClubBtnClick()
        };
        // 预加载游戏场景
        setTimeout(function(){
            cc.director.preloadScene("mjgame", function () {
                cc.log("mjgame preloaded")
            })
        },2000)   
    },
    
    refreshInfo:function(){
        var self = this;
        var onGet = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg)
            }else{
                if(ret.gems != null){
                    this.lblGems.string = ret.gems  
                }
            }
        };
        
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign
        };
        cc.vv.http.sendRequest("/get_user_status",data,onGet.bind(this))
    },
    
    refreshGemsTip:function(){
        var self = this;
        var onGet = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg)
            }else{
                cc.vv.userMgr.gemstip.version = ret.version;
                cc.vv.userMgr.gemstip.msg = ret.msg.replace("<newline>","\n")
            }
        };
        
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
            type:"fkgm",
            version:cc.vv.userMgr.gemstip.version
        };
        cc.vv.http.sendRequest("/get_message",data,onGet.bind(this))
    },
    
    refreshNotice:function(){
        var self = this;
        var onGet = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg)
            }else{
                cc.vv.userMgr.notice.version = ret.version;
                cc.vv.userMgr.notice.msg = ret.msg;
                this.lblNotice.string = ret.msg
            }
        };
        
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
            type:"notice",
            version:cc.vv.userMgr.notice.version
        };
        cc.vv.http.sendRequest("/get_message",data,onGet.bind(this))
    },
    
    initButtonHandler:function(btnPath){
        var btn = cc.find(btnPath);
        cc.vv.utils.addClickEvent(btn,this.node,"MjHall","onBtnClicked")      
    },
    
    initLabels:function(){
        this.lblName.string = cc.vv.userMgr.userName;
        this.lblGems.string = cc.vv.userMgr.gems;
        this.lblID.string = "ID:" + cc.vv.userMgr.userId
    },
    
    onBtnClicked:function(event){
        cc.vv.audioMgr.playSFX("select.mp3");
        if(event.target.name == "btn_shezhi"){
            this.settingsWin.active = true
        }else if(event.target.name == "btn_help"){
            this.helpWin.active = true
        }else if(event.target.name == "btn_xiaoxi"){
            this.xiaoxiWin.active = true
        }else if(event.target.name == "head"){
            cc.vv.userinfoShow.show(cc.vv.userMgr.userName,cc.vv.userMgr.userId,this.sprHeadImg,cc.vv.userMgr.sex,cc.vv.userMgr.ip)
        }
    },
    onClubBtnClick:function(){
        console.log("onClubBtnClick");
        this.node.getChildByName("julebu").active=true;
        // 先判断socket是否存在
        if (cc.vv.JuleNet.sio&&cc.vv.JuleNet.sio.connected) {
            // 拉取俱乐部列表
            cc.vv.JuleNet.send("get_club_list");
            // 拉取申请记录
            cc.vv.JuleNet.send("apply_list")
        }else{
            this.node.getChildByName("julebu").active=false;
            cc.vv.alert.show("提示","连接俱乐部失败，请重试");
            cc.vv.userMgr.connectClubServer()
        }
    },
    onReturnBighallClicked:function(){
        cc.vv.audioMgr.playSFX("select.mp3");
        cc.director.loadScene("bighall")
    },
    onJoinGameClicked:function(){
        cc.vv.audioMgr.playSFX("select.mp3");
        this.joinGameWin.active = true
    },
    
    onReturnGameClicked:function(){
        console.log("onReturnGameClicked");
        cc.vv.audioMgr.playSFX("select.mp3");
        cc.director.loadScene("mjgame")
    },
    
    onBtnAddGemsClicked:function(){
        cc.vv.alert.show("提示",cc.vv.userMgr.gemstip.msg);
        this.refreshInfo()
    },
    
    onCreateRoomClicked:function(){
        cc.vv.audioMgr.playSFX("select.mp3");
        if(cc.vv.gameNetMgr.roomId != null){
            cc.vv.alert.show("提示","房间已经创建!\n必须解散当前房间才能创建新的房间");
            return
        };
        cc.vv.userMgr.isCreate=false;
        // console.log("onCreateRoomClicked");
        this.createRoomWin.active = true
    },

    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        var x = this.lblNotice.node.x;
        x -= dt*100;
        if(x + this.lblNotice.node.width < -1000){
            x = 500
        };
        this.lblNotice.node.x = x;
        
        if(cc.vv && cc.vv.userMgr.roomData != null){
            cc.vv.userMgr.enterRoom(cc.vv.userMgr.roomData);
            cc.vv.userMgr.roomData = null
        };
        if (cc.vv.userMgr.gemsChange) {
            console.log("房卡有变化");
            if (cc.vv.userMgr.shareErrode!=0) {
                cc.vv.alert.show("提示",cc.vv.userMgr.shareErrmsg)
            }else{
                this.lblGems.string=cc.vv.userMgr.gems;
               
                cc.vv.alert.show("提示","分享成功,房卡已赠送")
            };
            cc.vv.userMgr.gemsChange=false
        };

        if(cc.vv.userMgr.sharteSuccess ){
            cc.vv.userMgr.sharteSuccess = false;
            cc.vv.userMgr.refreshGemsTip()
        }
    }
});
